import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/pages/teacher/evaluationBooks.dart';
import 'package:ichinesereader/pages/teacher/evaluationOverviewCharts.dart';
import 'package:ichinesereader/pages/teacher/evaluationRecords.dart';
import 'package:ichinesereader/pages/teacher/evaluationSkillCenter.dart';
import 'package:ichinesereader/pages/teacher/evaluationTests.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerTeacher.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/teacherSidebar.dart';
import '../../themes/appTheme.dart';

late TabController teacherEvaluationTabController;

class EvaluationHomepageForTeacher extends StatefulWidget {
  static const String routeName = '/teacher/evaluation';

  @override
  State<EvaluationHomepageForTeacher> createState() =>
      _EvaluationHomepageForTeacherState();
}

class _EvaluationHomepageForTeacherState
    extends State<EvaluationHomepageForTeacher>
    with SingleTickerProviderStateMixin {
  final box = Hive.box('appconfig');

  @override
  void initState() {
    teacherEvaluationTabController = TabController(vsync: this, length: 5);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher evaluation home page');

    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    final PageStyles _pageStyles = PageStyles();
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.teacherPageItems
        .indexWhere((page) => page.route == Get.currentRoute);

    return DefaultTabController(
      length: 5,
      child: Scaffold(
        drawer: isSmallScreen ? TeacherAppDrawer() : null,
        appBar: isSmallScreen ? CustomAppBar(title: 'Evaluation') : null,
        body: Container(
          decoration: _pageStyles.pageBoxDecoration(),
          child: Row(
            children: [
              Visibility(
                  visible: !isSmallScreen,
                  child: TeacherSidebarX(
                      selectedIndex: _selectedSidebarItem,
                      isBigScreen: isBigScreen)),
              Expanded(
                child: Container(
                  margin: _pageStyles.mainContainerPageEdgeInsets(),
                  child: Column(
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 9.0),
                        margin: EdgeInsets.only(bottom: 9.0),
                        decoration: BoxDecoration(
                          color: Get.isDarkMode
                              ? AppTheme.colors.darkTheme2
                              : Colors.black.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(3.0),
                        ),
                        child: _pageStyles.buildTabBarWithContoller([
                          'Books',
                          'Benchmark',
                          'Records',
                          'Overview',
                          'Skill Center'
                        ], teacherEvaluationTabController),
                      ),
                      Expanded(
                        flex: 9,
                        child: Container(
                          child: isTeacher
                              ? TabBarView(
                                  controller: teacherEvaluationTabController,
                                  children: [
                                    EvaluationBooksTeacherPage(),
                                    EvaluationTestsTeacherPage(),
                                    EvaluationRecordsForTeacher(),
                                    EvaluationOverviewChartsPage(),
                                    EvaluationSkillCenterForTeacher(),
                                  ],
                                )
                              : Text(''),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
